const { Sequelize, Model, INTEGER } = require('sequelize')
const { db } = require('../core/sequelize')
const moment = require('moment')
const { STRING } = require('sequelize')
class Tags extends Model {
    static async createTags(data) {
        const [user, created] = await Tags.findOrCreate({
            //查询是否存在
            where: {
                ...data
            },
            //不存在则插入数据库
            defaults: {
                ...data
            }
        })
        if (created) {
            return created
        } else {
            throw new Error('标签已存在')
        }
    }
    static async getTagsList(userid) {
        const res = await Tags.findAll({
            where: {
                userid
            }
        })
        return res
    }
    static async delTgt(data) {
        const res = await Tags.destroy({
            where: {
                ...data
            }
        })
        if (!res) {
            throw new Error('标签不存在')
        } else {
            return res
        }
    }
}
Tags.init(
    {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true, //设置主键
            autoIncrement: true,
        },
        createTime: { type: STRING, field: 'updateTime' },
        updateTime: { type: STRING, field: 'updateTime' },
        userid: INTEGER,
        label: STRING
    },
    {
        // underscored: true,
        sequelize: db,
        modelName: 'tags', timestamps: false
    }
)
module.exports = { Tags }
